package com.dempsey.auth.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.dempsey.api.domain.User;
import com.dempsey.api.model.LoginUser;
import com.dempsey.auth.form.LoginBody;
import com.dempsey.auth.form.RegisterBody;
import com.dempsey.auth.service.SysLoginService;
import com.dempsey.common.core.domain.AjaxResult;
import com.dempsey.common.security.service.TokenService;
import com.dempsey.common.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * token 控制
 *
 * @author Dempsey
 */
@RestController
@RequestMapping("/auth")
public class TokenController {

    private final static Log log = LogFactory.get();

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @PostMapping("/captcha")
    public AjaxResult captcha() {
        return sysLoginService.captcha();
    }

    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        log.info("用户登录：{}", loginBody);
        AjaxResult ajax = AjaxResult.success();
        // 用户登录
        LoginUser loginUser = sysLoginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCaptcha(), loginBody.getUUID());
        ajax.put("nickname", loginUser.getUser().getNickName());
        ajax.put("userId", loginUser.getUser().getUserId());
        // 获取登录token
        String token = tokenService.createToken(loginUser);
        ajax.put("token", token);
        return ajax;
    }

    @PostMapping("register")
    public AjaxResult register(@RequestBody RegisterBody registerBody) {
        log.info("用户注册：{}", registerBody);
        AjaxResult ajax = AjaxResult.success();

        String password = registerBody.getPassword();
        String encodePassword = new BCryptPasswordEncoder().encode(password);
        User user = User.builder().username(registerBody.getUsername()).password(encodePassword).nickName(registerBody.getNickName()).headImg(registerBody.getHeadImg()).build();
        String captcha = registerBody.getCaptcha();
        String uuid = registerBody.getUUID();
        // 用户注册
        LoginUser loginUser = sysLoginService.register(user, captcha, uuid);
        // 获取登录token
        String token = tokenService.createToken(loginUser);
        ajax.put("token", token);
        return ajax;
    }

    @PostMapping("/logout")
    public AjaxResult logout() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tokenService.deleteToken(loginUser);
        return AjaxResult.success("登出成功");
    }


}
